package com.voxel.sense.infomsg.module.common;

import android.text.TextUtils;
import android.view.View;

import com.example.com.common.app.BaseContract;
import com.example.com.common.app.PresenterFragment;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.voxel.sense.infomsg.R;

/**
 * Created by ChenYiYao on 2018/7/30.
 * 封装基类的recyclerView
 * 继承该类必须赋值empty_view和refresh_layout控件才能调用其中的方法
 */
public abstract class VRecyclerFrag<T extends BaseContract.Presenter, ViewMode> extends
        PresenterFragment<T> implements BaseContract.RecyclerView<T, ViewMode> {

    protected EmptyView empty_view;//占位的空界面布局
    protected SmartRefreshLayout refresh_layout;//下拉刷新头

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        empty_view = root.findViewById(R.id.empty_view);
        refresh_layout = root.findViewById(R.id.refresh_layout);
        //空界面绑定内容界面
        if (empty_view != null && refresh_layout != null) {
            empty_view.bind(refresh_layout);
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        empty_view.addRefreshBtnEvent(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                empty_view.triggerOk();
                refresh_layout.setEnableRefresh(true);
                boolean b = refresh_layout.autoRefresh();
                //刷新过于频繁会导致失败，不会回调刷新方法
                if (!b) {
                    showEmptyView("", true);
                }
            }
        });
    }

    @Override
    public void showEmptyView(String desc, boolean BtnVisible) {
        if (refresh_layout == null) {
            return;
        }
        empty_view.setRefreshBtnVisible(BtnVisible);
        boolean b = getRecyclerViewAdapter().getItemCount() > 0;
        if (b) {
            //发生错误，列表有数据，不能显示空界面
            super.showError(desc);
        } else if (!TextUtils.isEmpty(desc)) {
            empty_view.triggerError(desc);
        }  else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }


    @Override
    public void onShowNoMore(boolean b) {
        if (refresh_layout != null) {
            refresh_layout.setNoMoreData(b);
        }
    }

    @Override
    public void onRefreshFinish() {
        if (refresh_layout != null)
            refresh_layout.finishRefresh();

        if (empty_view != null)
            empty_view.triggerOkOrEmpty(getRecyclerViewAdapter().getItemCount() > 0 ? true : false);
    }

    @Override
    public void onLoadMoreFinish() {
        if (refresh_layout != null)
            refresh_layout.finishLoadMore();

        if (empty_view != null)
            empty_view.triggerOkOrEmpty(getRecyclerViewAdapter().getItemCount() > 0 ? true : false);
    }

    @Override
    public void onAdapterDataChanged() {

    }
}
